package com.graduation.mybatis.service.impl;

import com.alibaba.druid.wall.violation.ErrorCode;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.mybatis.bean.*;
import com.graduation.mybatis.mapper.*;
import com.graduation.mybatis.service.DietPlansService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 23726
* @description 针对表【diet_plans】的数据库操作Service实现
* @createDate 2025-04-22 11:56:47
*/
@Service
public class DietPlansServiceImpl extends ServiceImpl<DietPlansMapper, DietPlans>
    implements DietPlansService{

    @Resource
    private DietPlansMapper dietPlansMapper;
    @Resource
    private PlanFoodsMapper planFoodsMapper;

    public PlanFoods addFoodToPlan(AddFoodRequest request) {
        // 1. 查询用户的活动计划
        DietPlans activePlan = dietPlansMapper.selectActivePlanByUserId(request.getUserId());

        if (activePlan == null) {
            // 如果没有活动计划，创建一个新的计划
            activePlan = new DietPlans();
            activePlan.setUserId(request.getUserId());
            activePlan.setPlanName("默认计划");
            dietPlansMapper.insertNewPlan(activePlan);
        }
        // 2. 构造 PlanFoods 对象
        PlanFoods planFood = new PlanFoods();
        planFood.setPlanId(activePlan.getPlanId());
        planFood.setFoodId(request.getFoodId());
        planFood.setMealType(request.getMealType());
        planFood.setGrams(request.getGrams());

        planFoodsMapper.insertFoods(planFood);
        return planFood;

    }

    public boolean removePlanFoodById(Integer id) {
        try {
            planFoodsMapper.deleteByPlanFoodId(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    public List<DietRecordVO> getDietRecordsByUserId(Integer userId) {
        // 获取用户当前活跃计划
        DietPlans activePlan = dietPlansMapper.selectActivePlanByUserId(userId);
        if (activePlan == null) {
            return Collections.emptyList();
        }
        List<DietRecordVO> result = planFoodsMapper.selectDietRecordsByPlanId(activePlan.getPlanId());
        // 获取计划中的食物记录
        return result;
    }

    public NutritionSummary getPlanSummary(Integer userId) {
        // 1. 获取用户当前活跃计划
        DietPlans activePlan = dietPlansMapper.selectActivePlanByUserId(userId);
        if(activePlan == null)
        {
            return new NutritionSummary();
        }
        // 2. 获取计划中的食物记录
        List<DietRecordVO> dietRecords = planFoodsMapper.selectDietRecordsByPlanId(activePlan.getPlanId());

        if(dietRecords == null)
        {
            return new NutritionSummary();
        }
        // 3. 计算营养总和
        return calculateNutritionSummary(dietRecords);
    }

    private NutritionSummary calculateNutritionSummary(List<DietRecordVO> dietRecords) {
        NutritionSummary summary = new NutritionSummary();

        for (DietRecordVO record : dietRecords) {
            summary.addValues(
                    record.getActualCalories(),
                    record.getActualProtein(),
                    record.getActualFat(),
                    record.getActualCarbohydrate()
            );
        }

        return summary;
    }

}




